package com.amtf.datafile2.core.excel;

import com.amtf.datafile2.DataException;
import com.amtf.datafile2.cfg.excel.ExcelDataFieldDefinition;
import com.amtf.datafile2.core.std.StdDataField;

public class ExcelDataField extends StdDataField<ExcelDataFieldDefinition> {
	public ExcelDataField(ExcelDataFieldDefinition def) {
		super(def);
	}

	@Override
	public void setValue(String value) {
		if (value == null) {
			this.value = "";
		} else {
			if (definition.isTrimValue()) {
				this.value = value.trim();
			} else {
				this.value = value;
			}
		}
	}

	public void validate() {
		if (definition.isCheckFormat()) {
			this.clearErrors();
			if (!definition.isAllowEmpty() && value.length() == 0) {
				this.appendError("Empty value is not allowed.");
			}

			try {
				definition.getType().validate(this);
			} catch (DataException e) {
				this.appendError(e.getMessage());
			}
		}
	}

	@Override
	public String toString() {
		return value;
	}

}
